//
// Created by glt on 2025/3/16.
//

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
#include <iostream>
#include <string>
#include <vector>

struct ListNode {
    int val;
    ListNode *next;

    ListNode() : val(0), next(nullptr) {}

    explicit ListNode(int x) : val(x), next(nullptr) {}

    ListNode(int x, ListNode *next) : val(x), next(next) {}

    std::string to_show() {
        std::string result;
        ListNode *current = this;
        while (current != nullptr) {
            result += std::to_string(current->val);
            if (current->next != nullptr) {
                result += ",";
            }
            current = current->next;
        }
        return result;

    }
};


int search(std::vector<int> nums,int target){
    unsigned  long start = 0;
    unsigned  long end = nums.size()-1;
    unsigned  long middle;
    while (start<=end){
        if (start+1==end){
            if (nums[start]==target){
                return (int ) start;
            }
            if (nums[end]==target){
                return (int )end;
            }
            return -1;
        }
        middle = (end+start)/2;
        if (nums[middle] == target){
            return (int ) middle;
        }
        if (target< nums[middle]){
            end = middle;
        }else{
            start = middle;
        }
    }
    return  -1;
}

void for_search_target(){
    std::vector<int> nums={5};
    int target = 5;
    int result = search(nums,target);
    std::cout<<"for_search_target "<< target << " at index:"<<result<<std::endl;
}


ListNode *mergeTwoLists(ListNode *list1, ListNode *list2) {

    if (list1 == nullptr) {
        return list2;
    }
    if (list2 == nullptr) {
        return list1;
    }

    auto *result = new ListNode(0);

    ListNode *temp = result;

    while (list1->next != nullptr && list2->next != nullptr) {
        if (list1->val <= list2->val) {
            temp->next = list1;
            list1 = list1->next;
        } else {
            temp->next = list2;
            list2 = list2->next;
        }
        temp = temp->next;
    }

    while (list1->next != nullptr) {
        temp->next = list1;
        temp = temp->next;
        list1 = list1->next;
    }

    while (list2->next != nullptr) {
        temp->next = list2;
        temp = temp->next;
        list2 = list2->next;
    }
    return result->next;

}

void merge_list() {
    auto *listNode1 = new ListNode(1,
                                   new ListNode(2,
                                                new ListNode(4)));
    auto *listNode2 = new ListNode(1,
                                   new ListNode(3,
                                                new ListNode(4)));

    ListNode *listNode3 = mergeTwoLists(listNode1, listNode2);
    std::cout << "merge_list result " << listNode3->to_show() << std::endl;
}


ListNode *reverse_node(ListNode *origin) {
    if (origin == nullptr) {
        return origin;
    }
    ListNode *result = nullptr;
    ListNode *current = nullptr;

    while (origin != nullptr) {
        current = origin;
        origin = origin->next;
        current->next = result;
        result= current;
    }
    return result;

}

void reverse_node() {
    auto *listNode1 = new ListNode(1,
                                   new ListNode(2,
                                                new ListNode(4)));
    ListNode *result = reverse_node(listNode1);
    std::cout << "reverse_node result " << result->to_show() << std::endl;
}

int for_point_array(){
    std::string s = "abcd";
    for (char i : s) {
        std::cout<< i<< std::endl;
    }
    int a = 1;
    int c = 15;
    int  b = 2;
    int *p[2]; // 定义一个包含2个指向int类型的指针的数组
    p[0] = &a; // 让p[0]指向变量a
    p[1] = &b; // 让p[1]指向变量b

    std::cout << "p points to: " << p <<std::endl;
    std::cout << "p + 1 points to: " << p + 1 << std::endl;

    std::cout << "a address:" << &a << std::endl;
    std::cout << "b address: " << &b << std::endl;

    std::cout << "p[0] is: " << *p[0] << std::endl;; // 输出1
    std::cout << "p[1] is: " << *p[1] << std::endl;; // 输出2

    //int s=156;
    // 注意：直接打印指针地址可能会显示不同的结果，取决于系统环境
    return 0;
}

int main() {
    for_point_array();

    //for_search_target();

    //reverse_node();
    //merge_list();
    return 0;
}